#!/bin/sh

# if app not exist
if [ ! -f /bin/xupnpd ]; then
    exit 0
fi

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

LOG="logger -t xupnpd "

start() {
    get_param
    if [ "$xupnpd" = "1" ]; then
	# check ip adress set at wan
	wan_is_not_null=`ip -4 addr show $wan_if | grep inet -c`
	if [ "$wan_is_not_null" = "0" ]; then
    	    $LOG "Wait 10 sec before start..."
	    sleep 10
	fi
        $LOG "Start"
	export XUPNPDROOTDIR=/etc/xupnpd
	xupnpd &
    fi
}

get_param() {
    eval `nvram_buf_get 2860 xupnpd`
}

stop() {
    killall -q xupnpd
    killall -q -SIGKILL xupnpd
    rm -f /var/run/xupnpd.pid
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
